<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form ref="userInfoFrom" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- 使用 个人详情 组件 -->
            <UserInfo><UserInfo>
            </userinfo></userinfo></el-tab-pane>

          <el-tab-pane label="岗位信息">
            <!-- 使用 岗位信息 组件 -->
            <JobInfo></JobInfo>
          </el-tab-pane></el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetail } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
// 引入封装好的两个组件 个人详情和岗位信息
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  // 注册组件
  components: { UserInfo, JobInfo },

  data() {
    return {
      // 保存用户的id 不然不知道点了谁
      userId: this.$route.params.id,
      // 保存后台返回的员工数据信息
      userInfo: {
        username: '',
        password: ''
      },
      // 校验登录客户设置的数据
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          {
            min: 5, max: 12, message: '密码长度在5-12位', trigger: 'blur'
          }
        ]
      }
    }
  },

  async created() {
    this.getUserDetail()
  },

  methods: {
    // 获取员工的基本信息
    async getUserDetail() {
      this.userInfo = {
        ...await getUserDetail(this.userId),
        password: ''
      }
    },

    // 点击跟新
    async submit() {
      try {
        await this.$refs.userInfoFrom.validate()
        await saveUserDetailById(this.userInfo)
        this.$message.success('操作成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
